package com.srwl.mytx.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.Coupon;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.utils.Utils;
import com.srwl.mytx.activity.MultipleCouponDetailActivity;
import com.srwl.mytx.activity.SingleCouponDetailActivity;
import com.srwl.mytx.R;

import java.util.List;

/**
 * Created by max on 14/12/11
 */
public class CouponSendRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private boolean bol_noData;//标记是否有数据
    private boolean bol_requestError;//标记是否有数据
    private Context mContext;
    private List<Coupon> couponList;

    public CouponSendRecordAdapter(Context context, List<Coupon> couponList) {
        mContext = context;
        this.couponList = couponList;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.no__data__layout, parent, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.request_error_layout, parent, false));
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rp_send_record_list_item, parent, false);
        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            NoDataViewHolder noDataViewHolder = (NoDataViewHolder) holder;
            noDataViewHolder.tv_tips.setText("没有记录");
        } else if (viewType == Constant.ITEM_TYPE_NORMAL) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            setItemViews(itemViewHolder, position);
        }
    }


    private void setItemViews(ItemViewHolder itemViewHolder, int position) {
        Coupon coupon = couponList.get(position);
        itemViewHolder.tvDate.setText(DateUtils.getTimestampString(coupon.getCreated()));
       // coupon.setTargetUser(Helper.getInstance().getUserProfileManager().getLoginUserInfo());
        String status = "";
        if (coupon.getType() == Constant.COUPON_TYPE_ALI_FUND) {
            itemViewHolder.tvMoneyType.setText("支付宝红包");
        } else if (coupon.getType() == Constant.COUPON_TYPE_MULTIPLE_RANDOM) {
            itemViewHolder.tvMoneyType.setText("拼手气红包");
            coupon.setTargetUser(UserProfileManager.getInstance().getLoginUserInfo());
            itemViewHolder.itemView.setOnClickListener(v -> mContext.startActivity(new Intent(mContext, MultipleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_COUPON, coupon)));
            status = showMultipleCouponStatus(coupon.getStatus());
        } else if (coupon.getType() == Constant.COUPON_TYPE_MULTIPLE_AVERAGE) {
            itemViewHolder.tvMoneyType.setText("均额红包");
            status = showMultipleCouponStatus(coupon.getStatus());
            coupon.setTargetUser(UserProfileManager.getInstance().getLoginUserInfo());
            itemViewHolder.itemView.setOnClickListener(v -> mContext.startActivity(new Intent(mContext, MultipleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_COUPON, coupon)));
        } else {
            itemViewHolder.tvMoneyType.setText("普通红包");
            status = showSingleCouponStatus(coupon.getStatus());
            coupon.setSendMessage(true);//标记这是发送的红包，在红包详情里面，红包发送者和接收者，看到的页面是不一样的
            itemViewHolder.itemView.setOnClickListener(v -> mContext.startActivity(new Intent(mContext, SingleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_COUPON, coupon)));
        }
        itemViewHolder.tvMoney.setText(Utils.moneyFormat(coupon.getAmount()));
        itemViewHolder.tvMoneyStatus.setText(status);

    }


    @NonNull
    private String showSingleCouponStatus(Integer status) {
        if (status == null) {
            return "";
        }
        String str_status;
        if (status == Constant.COUPON_STATUS_RECEIVED) {
            str_status = "已领取";
        } else if (status == Constant.COUPON_STATUS_RECEIVABLE) {
            str_status = "未领取";
        } else {
            str_status = "已过期";
        }
        return str_status;
    }

    @NonNull
    private String showMultipleCouponStatus(Integer status) {
        if (status == null) {
            return "";
        }
        String str_status;
        if (status == Constant.COUPON_STATUS_REMAIN_ZERO) {
            str_status = "已领完";
        } else if (status == Constant.COUPON_STATUS_RECEIVABLE) {
            str_status = "未领完";
        } else {
            str_status = "已过期";
        }
        return str_status;
    }

    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return couponList.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        }
        return Constant.ITEM_TYPE_NORMAL;
    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }


    private static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvMoneyType;
        TextView tvMoney;
        TextView tvDate;
        TextView tvMoneyStatus;
        TextView tvMerchantName;


        ItemViewHolder(View v) {
            super(v);
            initView(v);
        }

        private void initView(View v) {
            tvMoneyType = v.findViewById(R.id.tv_money_type);
            tvMoney = v.findViewById(R.id.tv_item_money_amount);
            tvDate = v.findViewById(R.id.tv_time);
            tvMoneyStatus = v.findViewById(R.id.tv_item_status);
            tvMerchantName = v.findViewById(R.id.tv_merchant_name);

        }
    }

}
